Skip to main content

User interactions

Automated testing of 3D applications often requires simulating complex user interactions such as dragging, rotating, and zooming. These interactions can be scripted using tools like Playwright, which provides APIs for simulating mouse and keyboard actions. Below, we explore how to simulate such interactions and best practices to ensure realistic and reliable tests.


Simulating 3D Interactions with Playwright

Example: Simulating Rotation

Rotation in 3D space is typically achieved by clicking and dragging. Using Playwright, we can script this interaction as follows:

(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto("https://your-3d-app-url");

// Wait for the 3D canvas to load
await page.waitForSelector("#3d-canvas");

// Simulate rotating (left-click drag)
const canvas = await page.$("#3d-canvas");
const boundingBox = await canvas.boundingBox();

const startX = boundingBox.x + boundingBox.width / 2;
const startY = boundingBox.y + boundingBox.height / 2;

await page.mouse.move(startX, startY);
await page.mouse.down({ button: "left" });
await page.mouse.move(startX + 100, startY, { steps: 10 });
await page.mouse.up({ button: "left" });

await browser.close();
})();

Example: Simulating Zoom

Zooming is often performed using a scroll wheel. Here's how to simulate zooming in and out:

(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto("https://your-3d-app-url");

// Wait for the 3D canvas to load
await page.waitForSelector("#3d-canvas");

// Simulate zooming (scroll wheel)
const canvas = await page.$("#3d-canvas");
const boundingBox = await canvas.boundingBox();

const centerX = boundingBox.x + boundingBox.width / 2;
const centerY = boundingBox.y + boundingBox.height / 2;

await page.mouse.move(centerX, centerY);
await page.mouse.wheel(0, -500); // Zoom in
await page.mouse.wheel(0, 500); // Zoom out

await browser.close();
})();

Best Practices for Simulating User Inputs

  1. Realistic Timings and Delays
    Introduce delays and randomness to mimic human interaction. Avoid overly fast or robotic movements.

  2. Smooth and Curved Mouse Movements
    Use intermediate steps to create smooth transitions between points. This ensures more natural interactions.

  3. Account for Dynamic Content
    Wait for 3D elements to fully render before interacting with them. This avoids flaky tests caused by incomplete loading.

  4. Consistency in Complex Interactions
    Standardize complex interactions (e.g., drag-and-drop, zoom) across tests by creating reusable utility functions.

  5. Stable Testing Environment
    Ensure the testing environment is consistent, including screen resolution, browser version, and network conditions.


By following these guidelines and leveraging Playwright's powerful API, you can create robust automated tests for 3D applications that simulate realistic user interactions. This ensures higher confidence in the reliability and usability of your 3D application.